home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / strassoc.d < prev    next >
Text File  |  1996-03-27  |  3KB  |  167 lines

  1.  
  2. /*
  3.  *
  4.  *    Copyright (c) 1993-1996 Algorithms Corporation
  5.  *    3020 Liberty Hills Drive
  6.  *    Franklin, TN  37067
  7.  *
  8.  *    ALL RIGHTS RESERVED.
  9.  *
  10.  *
  11.  *
  12.  */
  13.  
  14.  
  15. #include <string.h>
  16. #include <math.h>
  17. #include "memalloc.h"
  18.  
  19.  
  20. defclass  StringAssociation : Association  {
  21.     char    *iKey;
  22.     iValue;
  23. };
  24.  
  25. cmeth    gNewWithStrObj, <vNew> (char *key, value)
  26. {
  27.     int    len = key ? strlen(key) : 0;
  28.     object    assoc;
  29.     ivType    *iv;
  30.  
  31.     ChkArgNul(value, 3);
  32.     assoc = gNew(super);
  33.     iv = ivPtr(assoc);
  34.     iKey = (char *) MA_malloc(len+1, &iKey);
  35.     strcpy(iKey, key ? key : "");
  36.     iValue = value;
  37.     return assoc;
  38. }
  39.  
  40. imeth    gDeepCopy()
  41. {
  42.     object    nobj;
  43.     ivType    *iv2;
  44.  
  45.     nobj = gDeepCopy(super);
  46.     iv2 = ivPtr(nobj);
  47.     if (iKey)  {
  48.         iv2->iKey = (char *) MA_malloc(strlen(iKey)+1, &iv2->iKey);
  49.         strcpy(iv2->iKey, iKey);
  50.     }
  51.     if (iValue)
  52.         iv2->iValue = gDeepCopy(iValue);
  53.     return nobj;
  54. }
  55.  
  56. imeth    gCopy()
  57. {
  58.     object    nobj;
  59.     ivType    *iv2;
  60.  
  61.     nobj = gCopy(super);
  62.     iv2 = ivPtr(nobj);
  63.     if (iKey)  {
  64.         iv2->iKey = (char *) MA_malloc(strlen(iKey)+1, &iv2->iKey);
  65.         strcpy(iv2->iKey, iKey);
  66.     }
  67.     iv2->iValue = iValue;
  68.     return nobj;
  69. }
  70.  
  71. imeth    gValue()
  72. {
  73.     return iValue;
  74. }
  75.  
  76. imeth    gChangeValue(value)
  77. {
  78.     object    ret;
  79.     ChkArgNul(value, 2);
  80.     ret = iValue;
  81.     iValue = value;
  82.     return ret;
  83. }
  84.  
  85. imeth    char    *gStringKey()
  86. {
  87.     return iKey;
  88. }
  89.  
  90. imeth    gChangeStringKey(char *key)
  91. {
  92.     int    len = key ? strlen(key) : 0;
  93.  
  94.     if (iKey)  {
  95.         if (len > (int) strlen(iKey))
  96.             iKey = (char *) MA_realloc(iKey, len+1);
  97.     } else
  98.         iKey = (char *) MA_malloc(len+1, &iKey);
  99.     strcpy(iKey, key ? key : "");
  100.     return self;
  101. }
  102.  
  103. imeth    object    gDispose, gGCDispose ()
  104. {
  105.     if (iKey)
  106.         MA_free(iKey);
  107.     return gDispose(super);
  108. }
  109.  
  110. imeth    object    gDeepDispose()
  111. {
  112.     if (iKey)
  113.         MA_free(iKey);
  114.     if (iValue)
  115.         gDeepDispose(iValue);
  116.     return gDeepDispose(super);
  117. }
  118.  
  119. imeth    gStringRepValue()
  120. {
  121.     object    s, t;
  122.     
  123.     t = iValue ? gStringRepValue(iValue) : (object) NULL;
  124.     s = vSprintf(String, "( \"%s\", ", iKey ? iKey : "(null)");
  125.     vBuild(s, NULL, t ? (char *) t : "(null)", " )", NULL);
  126.     if (t)
  127.         gDispose(t);
  128.     return s;
  129. }
  130.  
  131. imeth    int    gHash()
  132. {
  133.     register char     c = 'a';
  134.     char    *s = iKey;
  135.     double    t;
  136.     register unsigned short     k=0;  /* must be short     */
  137.  
  138.     if (s)
  139.         while (*s)
  140.             k += *s++ ^ c++;
  141.     t = .6125423371    * k;
  142.     t = t < 0.0 ? -t : t;
  143.     return (int) (BIG_INT * (t - floor(t)));
  144. }
  145.  
  146. imeth    int    gCompare(arg)
  147. {
  148.     ChkArg(arg, 2);
  149.     return strcmp(iKey?iKey:"", gStringKey(arg));
  150. }
  151.  
  152.  
  153.  
  154.  
  155. /*
  156.  *
  157.  *    Copyright (c) 1993-1996 Algorithms Corporation
  158.  *    3020 Liberty Hills Drive
  159.  *    Franklin, TN  37067
  160.  *
  161.  *    ALL RIGHTS RESERVED.
  162.  *
  163.  *
  164.  *
  165.  */
  166.  
  167.